package com.data.center.service.impl;

import com.data.center.dictionary.OrganizationTagRelationshipEnum;
import com.data.center.entity.OrganizationTag;
import com.data.center.entity.OrganizationTagRelationship;
import com.data.center.exception.ResultBody;
import com.data.center.exception.exception.BizException;
import com.data.center.mapper.OrganizationTagMapper;
import com.data.center.mapper.OrganizationTagRelationshipMapper;
import com.data.center.service.OrganizationTagRelationshipService;
import com.data.center.service.OrganizationTagService;
import com.data.center.util.ForeignManage.ForeignDataUtil;
import com.data.center.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * @author Li_Jx
 * @description:
 * @date 2023/11/30 17:47
 */
@Service
public class OrganizationTagRelationshipServiceImpl implements OrganizationTagRelationshipService {
    @Autowired
    private OrganizationTagRelationshipMapper organizationTagRelationshipMapper;
    @Autowired
    private OrganizationTagMapper organizationTagMapper;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private ForeignDataUtil foreignDataUtil;




    @Override
    public void update(OrganizationTagRelationship organizationTagRelationship) throws RuntimeException{
        String id = organizationTagRelationship.getId();
        String headTag = organizationTagRelationship.getHeadTagId();
        String tailTag = organizationTagRelationship.getTailTagId();
        String type = organizationTagRelationship.getType();
        if(id == null || id.isEmpty()){
            throw new RuntimeException("标签id为空");
        }
        OrganizationTagRelationship byId = organizationTagRelationshipMapper.findById(id);
        if (byId == null){
            throw new RuntimeException("此id无对应数据");
        }

        if (headTag != null){
            List<OrganizationTag> headTagId = organizationTagMapper.findById(headTag);
            if (headTagId.size() == 0){
                throw new RuntimeException("头标签id无对应数据");
            }
        }
        if (tailTag != null){
            List<OrganizationTag> tailTagId = organizationTagMapper.findById(tailTag);
            if (tailTagId.size() == 0){
                throw new RuntimeException("尾标签id无对应数据");
            }
        }
        if (type != null){
            OrganizationTagRelationshipEnum valueByKey = OrganizationTagRelationshipEnum.getValueByKey(type);
            if (valueByKey == null){
                throw new RuntimeException("输入的type为无效值");
            }
        }
        //查查有没有重复数据
        OrganizationTagRelationship testObject = new OrganizationTagRelationship();
        testObject.setHeadTagId(headTag);
        testObject.setTailTagId(tailTag);
        testObject.setType(type);
        List<OrganizationTagRelationship> select = organizationTagRelationshipMapper.select(testObject);
        if (select.size() != 0){
            throw new RuntimeException("此数据已存在");
        }
        organizationTagRelationship.setModifier("");
        organizationTagRelationship.setLastModifiedTime(new Date());
        organizationTagRelationshipMapper.update(organizationTagRelationship);
    }

    @Override
    public void insert(OrganizationTagRelationship organizationTagRelationship) throws RuntimeException {
        String headTag = organizationTagRelationship.getHeadTagId();
        String tailTag = organizationTagRelationship.getTailTagId();
        String type = organizationTagRelationship.getType();

        if (headTag == null || tailTag == null || type == null){
            throw new RuntimeException("待插入数据存在空值");
        }
        List<OrganizationTag> headTagId = organizationTagMapper.findById(headTag);
        if (headTagId.size() == 0){
            throw new RuntimeException("头标签id无对应数据");
        }

        List<OrganizationTag> tailTagId = organizationTagMapper.findById(tailTag);
        if (tailTagId.size() == 0){
            throw new RuntimeException("尾标签id无对应数据");
        }

        OrganizationTagRelationshipEnum valueByKey = OrganizationTagRelationshipEnum.getValueByKey(type);
        if (valueByKey == null){
            throw new RuntimeException("输入的type为无效值");
        }

        //查查有没有重复数据
        OrganizationTagRelationship testObject = new OrganizationTagRelationship();
        testObject.setHeadTagId(headTag);
        testObject.setTailTagId(tailTag);
        testObject.setType(type);
        List<OrganizationTagRelationship> select = organizationTagRelationshipMapper.select(testObject);
        if (select.size() != 0){
            throw new RuntimeException("此数据已存在");
        }
        //插入数据
        organizationTagRelationship.setId(String.valueOf(snowflakeIdWorker.nextId()));
        organizationTagRelationship.setCreator("");
        organizationTagRelationship.setCreateTime(new Date());
        organizationTagRelationship.setModifier("");
        organizationTagRelationship.setLastModifiedTime(new Date());
        organizationTagRelationshipMapper.insert(organizationTagRelationship);
    }

    @Override
    public List<OrganizationTagRelationship> searchByForeignKey(String id) {
        OrganizationTagRelationship headFilter = new OrganizationTagRelationship();
        headFilter.setHeadTagId(id);
        OrganizationTagRelationship tailFilter = new OrganizationTagRelationship();
        tailFilter.setTailTagId(id);
        List<OrganizationTagRelationship> selectHead = organizationTagRelationshipMapper.select(headFilter);
        List<OrganizationTagRelationship> selectTail = organizationTagRelationshipMapper.select(tailFilter);
        ArrayList<OrganizationTagRelationship> organizationTagRelationships = new ArrayList<>();
        organizationTagRelationships.addAll(selectHead);
        organizationTagRelationships.addAll(selectTail);
        Set<OrganizationTagRelationship> set = new HashSet<>(organizationTagRelationships);
        List<OrganizationTagRelationship> resultList = new ArrayList<>(set);
        return resultList;

    }

    @Override
    public int deleteByForeignKey(String id) {
        List<OrganizationTagRelationship> organizationTagRelationshipList = searchByForeignKey(id);
        if (organizationTagRelationshipList.size() == 0){
            return 0;
        }
        for (OrganizationTagRelationship item : organizationTagRelationshipList) {
            organizationTagRelationshipMapper.deleteById(item.getId());
        }
        return 0;
    }

    @Override
    public int mergeByForeignKey(String id, String deletedId) {
        if (id.equals(deletedId)){
            return  0;
        }
        OrganizationTagRelationship headFilter = new OrganizationTagRelationship();
        headFilter.setHeadTagId(deletedId);
        OrganizationTagRelationship tailFilter = new OrganizationTagRelationship();
        tailFilter.setTailTagId(deletedId);
        List<OrganizationTagRelationship> selectHead = organizationTagRelationshipMapper.select(headFilter);
        List<OrganizationTagRelationship> selectTail = organizationTagRelationshipMapper.select(tailFilter);
        if (selectHead.size() > 0) {
            for (OrganizationTagRelationship item : selectHead) {
                if (id.equals(item.getTailTagId())) {
                    throw new RuntimeException("头尾标签不能相同");
                }
                item.setHeadTagId(id);
                update(item);
            }
        }
        if (selectTail.size() > 0) {
            for (OrganizationTagRelationship item : selectTail) {
                if (id.equals(item.getHeadTagId())) {
                    throw new RuntimeException("头尾标签不能相同");
                }
                item.setTailTagId(id);
                update(item);
            }
        }
        return 0;
    }

}
